package com.example.demo9.servlet;
import com.example.demo9.mapper.CartMapper;
import com.example.demo9.pojo.Cart;
import com.example.demo9.utils.MybatisUtils;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "Cart",value = "/cartadd")
public class CartSevrlet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
         Gson gson= new Gson();
         BufferedReader br=new BufferedReader(request.getReader());
         StringBuffer stb=new StringBuffer();
         String line;
         while ((line= br.readLine())!=null){
             stb.append(line);
         }
        Cart cart=gson.fromJson(stb.toString(), Cart.class);
        SqlSession sqlSession= MybatisUtils.getSession();
        CartMapper mapper=sqlSession.getMapper(CartMapper.class);
        int cart1=mapper.addCart(cart);
        sqlSession.commit();
        HashMap<String,Object> map=new HashMap<>();
        if (cart1 > 0) {
            map.put("msg","添加成功");
            map.put("code",200);
            map.put("data",cart);

        }else {
            map.put("msg","添加失败");
            map.put("code",500);
        }
        resp.setStatus(cart1 > 0 ? 200 : 500);
        resp.getWriter().write(gson.toJson(map));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
     String  method=  req.getParameter("method");
        if ("cha".equals(method)) {
            cha(req,resp);

        } else if ("del".equals(method)) {
            del(req,resp);
            
        }

    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        Gson gson =new Gson();
        String pid=req.getParameter("pid");
        String userid=req.getParameter("userid");
        SqlSession sqlSession=MybatisUtils.getSession();
        CartMapper mapper=sqlSession.getMapper(CartMapper.class);
        int num=Integer.parseInt(pid);
       int res=mapper.delCartByPid(num,userid);
        sqlSession.commit();
        HashMap<String,Object> map=new HashMap<>();
        if (res >0) {
            map.put("msg","删除成功");
            map.put("code",200);
            map.put("data",res);
        }else {
            map.put("msg","删除失败");
            map.put("code",500);
        }
        resp.getWriter().append(gson.toJson(map));
    }

    private void cha(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        Gson gson= new Gson();
        String userid=req.getParameter("userid");
        SqlSession sqlSession=MybatisUtils.getSession();
        CartMapper mapper=sqlSession.getMapper(CartMapper.class);
        List<Cart> cart=mapper.getCartByUserid(userid);
        HashMap<String,Object> map=new HashMap<>();
        if (!cart.isEmpty()) {
            map.put("msg","查询成功");
            map.put("code","200");
            map.put("data",cart);
        }else {
            map.put("msg","暂无改用户信息");
            map.put("code","500");
        }
        resp.getWriter().append(gson.toJson(map));
    }

}
